package com.warmlight.voicepacket.adapter.comm;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.lzy.ninegrid.ImageInfo;
import com.lzy.ninegrid.NineGridView;
import com.lzy.ninegrid.preview.NineGridViewClickAdapter;
import com.warmlight.voicepacket.CommentDetailAcitivity;
import com.warmlight.voicepacket.R;
import com.warmlight.voicepacket.bean.cdetails.CDetailsListBean;
import com.warmlight.voicepacket.ui.user.LoginRegistActivity;
import com.warmlight.voicepacket.ui.user.usercenter.LookUserCenterActivity;
import com.warmlight.voicepacket.utils.SPUtils;
import com.warmlight.voicepacket.utils.UIUtils;
import com.warmlight.voicepacket.widget.ReportCopaPopWindow;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by weiweijinpeng on 2018/9/15.
 * 帖子的详情适配器
 */

public class CommDetailsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public static final int ITEM_TYPE_NORMAL = 1;
    public static final int ITEM_TYPE_PICTURE = 2;

    private Context mContext;

    public List<CDetailsListBean> commBeans;

//    private CDetailsListBean commList;

    private LayoutInflater inflater;

    private VideoItemListener focusItemListener;

    public CommDetailsAdapter(Context context,VideoItemListener focusItemListener){

        this.mContext=context;
        this.focusItemListener=focusItemListener;
        inflater = LayoutInflater.from(context);
        commBeans=new ArrayList<>();
    }

    public void addData(List<CDetailsListBean> mData) {
        if (mData!=null){
            commBeans.addAll(mData);
            notifyDataSetChanged();
        }
    }

    public void setData(List<CDetailsListBean> mData) {
        if (mData!=null){
            commBeans.clear();
            commBeans.addAll(mData);
            notifyDataSetChanged();
        }
    }

    public void addItem(CDetailsListBean community) {
        if (commBeans != null) {
            commBeans.add(0,community);
            notifyDataSetChanged();
        }
    }

    public List<CDetailsListBean> getData(){
        return commBeans;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = null;
        View view;
        switch (viewType) {
            case ITEM_TYPE_NORMAL:
                view = inflater.inflate(R.layout.item_cdetails_normal, parent, false);
                viewHolder = new CommDetailsAdapter.FocusNormalViewHolder(view);
                break;
            case ITEM_TYPE_PICTURE:
                view = inflater.inflate(R.layout.item_cdetails_picture, parent, false);
                viewHolder = new CommDetailsAdapter.FocusPictureViewHolder(view);
                break;
            default:
                throw new IllegalArgumentException("Invalid viewType");
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        if (holder instanceof CommDetailsAdapter.FocusNormalViewHolder) {
            FocusNormalViewHolder normalViewHolder=(FocusNormalViewHolder) holder;

            CDetailsListBean commList=commBeans.get(position);

            UIUtils.loadImage(normalViewHolder.mUserIcon,commList.getUser().getAvator());

            normalViewHolder.mUserIcon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, LookUserCenterActivity.class);
                    intent.putExtra("uid",commBeans.get(position).getUser().getUid());
                    intent.putExtra("type","video");
                    mContext.startActivity(intent);
                }
            });

            normalViewHolder.mUserNickName.setText(commList.getUser().getNickname());

            normalViewHolder.mUserNickName.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, LookUserCenterActivity.class);
                    intent.putExtra("uid",commBeans.get(position).getUser().getUid());
                    intent.putExtra("type","video");
                    mContext.startActivity(intent);
                }
            });

            String content = commList.getContent();
            if (!TextUtils.isEmpty(content)){
                normalViewHolder.mRecommContent.setText(content);
                normalViewHolder.mRecommContent.setVisibility(View.VISIBLE);
            }else{
                normalViewHolder.mRecommContent.setText(content);
                normalViewHolder.mRecommContent.setVisibility(View.GONE);
            }

            normalViewHolder.mCommDate.setText(commList.getDate());

            String priaise = commList.getLike_num();
//            String priaise = FormUnitUtils.form(BigDecimal.valueOf(commList.getLike_num()), "w");
//            if (TextUtils.equals("0",priaise)){
//                priaise = "点赞";
//            }
            normalViewHolder.mCommLike.setText(priaise);

            normalViewHolder.mCommLike.setTag(position);

            if (TextUtils.equals(commList.getIs_god(),"1")){
                normalViewHolder.iv_glodcomment.setVisibility(View.VISIBLE);
            }else {
                normalViewHolder.iv_glodcomment.setVisibility(View.GONE);
            }


//            isLike(normalViewHolder,position);
            isLike(normalViewHolder.mCommLike,position);

            if (TextUtils.isEmpty(commList.getReply_num())||commList.getReply_num().equals("0")){
                normalViewHolder.tv_replynum.setText("回复");
                normalViewHolder.iv_comententry.setVisibility(View.GONE);
            }else {
                normalViewHolder.tv_replynum.setText(commList.getReply_num()+"条回复");
            }

            final List<Boolean> gif_array=new ArrayList<>();

            for (int i=0;i<commList.getPics().size();i++){
                gif_array.add(commList.getPics().get(i).isIs_gif());
            }

            ArrayList<ImageInfo> imageInfo = new ArrayList<>();
            for (int i = 0; i < commList.getPics().size(); i++) {
                ImageInfo info = new ImageInfo();
                info.setThumbnailUrl(commList.getPics().get(i).getU());
                info.setBigImageUrl(commList.getPics().get(i).getU());
                imageInfo.add(info);
            }
            normalViewHolder.nineGrid.setSingleImageSize(400);
            normalViewHolder.nineGrid.setAdapter(new NineGridViewClickAdapter(mContext, imageInfo));


        }else if (holder instanceof CommDetailsAdapter.FocusPictureViewHolder){
            FocusPictureViewHolder pictureViewHolder=(FocusPictureViewHolder) holder;

        }
    }

    /**
     * 帖子的点赞状态
     * @param
     */
    public void isLike(TextView like, int position){
        CDetailsListBean commList=commBeans.get(position);

        Drawable nav_up;
        if (commList.getIs_like()==1){
            nav_up = mContext.getResources().getDrawable(R.drawable.icon_community_zan_true);
            nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
            like.setCompoundDrawables(nav_up, null, null, null);

        }else if (commList.getIs_like()==0){
            nav_up = mContext.getResources().getDrawable(R.drawable.icon_community_zan_false);
            nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
            like.setCompoundDrawables(nav_up, null, null, null);
        }
//        String priaise = FormUnitUtils.form(BigDecimal.valueOf(commList.getLike_num()), "w");
//        if (TextUtils.equals("0",priaise)){
//            priaise = "点赞";
//        }
//        verticalHolder.mCommLike.setText(priaise);

    }

    /**
     * 帖子的点赞状态
     * @param
     */
    public void isLike( int position){
//        CDetailsListBean commList=commBeans.get(position);
//
//        Drawable nav_up;
//        if (commList.getIs_like()==1){
//            nav_up = mContext.getResources().getDrawable(R.drawable.icon_community_zan_true);
//            nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
//            like.setCompoundDrawables(nav_up, null, null, null);
//
//        }else if (commList.getIs_like()==0){
//            nav_up = mContext.getResources().getDrawable(R.drawable.icon_community_zan_false);
//            nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
//            like.setCompoundDrawables(nav_up, null, null, null);
//        }
//        String priaise = FormUnitUtils.form(BigDecimal.valueOf(commList.getLike_num()), "w");
//        if (TextUtils.equals("0",priaise)){
//            priaise = "点赞";
//        }
//        verticalHolder.mCommLike.setText(priaise);

        notifyItemChanged(position);
    }

    @Override
    public int getItemCount() {
        return commBeans.size();
    }

    @Override
    public int getItemViewType(int position) {

        return ITEM_TYPE_NORMAL;
    }


    public class FocusNormalViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        @BindView(R.id.user_icon)
        SimpleDraweeView mUserIcon;

        @BindView(R.id.gridView_picarr)
        NineGridView nineGrid;

        @BindView(R.id.recomm_nickname)
        TextView mUserNickName;

        @BindView(R.id.recomm_content)
        TextView mRecommContent;

        @BindView(R.id.comm_date)
        TextView mCommDate;

        @BindView(R.id.comm_like)
        TextView mCommLike;

        @BindView(R.id.tv_replynum)
        TextView tv_replynum;

        @BindView(R.id.iv_comententry)
        ImageView iv_comententry;

        @BindView(R.id.iv_glodcomment)
        ImageView iv_glodcomment;


        FocusNormalViewHolder(View itemView){
            super(itemView);
            ButterKnife.bind(this, itemView);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent=new Intent(mContext, CommentDetailAcitivity.class);
                    intent.putExtra("post_id",commBeans.get(getPosition()).getPost_id());
                    intent.putExtra("reply_id",commBeans.get(getPosition()).getId());
                    intent.putExtra("uid",commBeans.get(getPosition()).getUser().getUid());
                    mContext.startActivity(intent);
                }
            });
            itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    ReportCopaPopWindow popWindow = new ReportCopaPopWindow(mContext);
                    popWindow.setID(commBeans.get(getPosition()).getId(), commBeans.get(getPosition()).getContent());
                    popWindow.showPopupWindow(mCommLike);
                    return false;
                }
            });
        }

        @OnClick({R.id.comm_like})
        @Override
        public void onClick(View v) {
            int position= (int) v.getTag();
            switch (v.getId()){
                case R.id.comm_like:
                    if (! SPUtils.isLogined()) {
                        LoginRegistActivity.startLoginRegistActivity(mContext);
                        return;
                    }
                    focusItemListener.isLikeClick(position);
                    break;
            }
        }
    }

    public class FocusPictureViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{


        FocusPictureViewHolder(View itemView){
            super(itemView);
            ButterKnife.bind(this, itemView);

        }

        @Override
        public void onClick(View v) {
            int position= (int) v.getTag();
        }
    }

    public interface VideoItemListener{
        void isLikeClick(int position);
    }
}

